class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> list=new ArrayList<>();
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        if(root!=null){
            while(!queue.isEmpty()){
                int size=queue.size();
                List<Integer> ret=new ArrayList<>();
                while(size!=0){
                    TreeNode tmp=queue.poll();
                    ret.add(tmp.val);
                    if(tmp.left!=null){
                        queue.offer(tmp.left);
                    }
                    if(tmp.right!=null){
                        queue.offer(tmp.right);
                    }
                    size--;
                }
                list.add(ret);
            }
        }
        return list;
    }
}
